package com.lvt.example.thinking.in.spring.questions.L22.web.example04.validate;

import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * desc：class
 *
 * @author LT
 * @date 2022/10/24
 */
@Slf4j
@Validated
@RestController
public class ValidateController {

    /**
     * 案例 1：对象参数校验失效
     * HandlerMethodArgumentResolverComposite#getArgumentResolver
     * 找到 Resolver 后，就会执行 HandlerMethodArgumentResolver#resolveArgument 方法。它首先会根据当前的请求（NativeWebRequest）组装出 Student 对象并对这个对象进行必要的校验，
     * 校验的执行参考 AbstractMessageConverterMethodArgumentResolver#validateIfApplicable
     */
    @RequestMapping(path = "students", method = RequestMethod.POST)
    public void addStudent(@RequestBody Student student) {
        log.info("add new student: {}", student.toString());
    }

    @RequestMapping(path = "students/fix1", method = RequestMethod.POST)
    public void addStudentFix1(@Validated @RequestBody Student student) {
        log.info("add new student: {}", student.toString());
    }

    @RequestMapping(path = "students/fix2", method = RequestMethod.POST)
    public void addStudentFix2(@Valid @RequestBody Student student) {
        log.info("add new student: {}", student.toString());
    }

    @RequestMapping(path = "students/fix3", method = RequestMethod.POST)
    public void addStudentFix3(@ValidCustomized @RequestBody Student student) {
        log.info("add new student: {}", student.toString());
    }

    /**
     * 案例 2：嵌套校验失效
     * 在组装 BeanMetaData 过程中，会根据成员字段是否标记了 @Valid 来决定（记录）这个字段以后是否做级联校验，
     * 参考代码 AnnotationMetaDataProvider#getCascadingMetaData
     */
    @RequestMapping(path = "student/2", method = RequestMethod.POST)
    public void addStudent2(@ValidCustomized @RequestBody Student student) {
        log.info("add new student: {}", student.toString());
    }

    /**
     * 案例 3：误解校验执行
     */


}
